package cc.mrbird.febs.business.controller;

import cc.mrbird.febs.business.domain.Store;
import cc.mrbird.febs.business.service.StoreFileService;
import cc.mrbird.febs.business.service.StoreService;
import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("store")
@Api(description = "商家接口")
public class StoreController extends BaseController {

    @Autowired
    private StoreService storeService;

    @Autowired
    private StoreFileService storeFileService;

    private String message;

    @GetMapping
    @RequiresPermissions("store:view")
    @ApiOperation(value = "商户列表" ,  notes="商户列表")
    public Map<String, Object> storeList(QueryRequest queryRequest, Store store) {
        return getDataTable(storeService.findStores(store, queryRequest));
    }

    @GetMapping("check/{storeName}")
    @ApiOperation(value = "商户列表" ,  notes="商户列表")
    public boolean checkStoreName(@NotBlank(message = "{required}") @PathVariable String storeName) {
        Store result = this.storeService.findByName(storeName);
        return result == null;
    }

    @Log("新增商户")
    @PostMapping
    @RequiresPermissions("store:add")
    public void addStore(@Valid @ApiParam("商户对象") @RequestBody Store store) throws FebsException {
        try {
            this.storeService.createStore(store);
        } catch (Exception e) {
            message = "新增商户失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除商户")
    @DeleteMapping("/{storeIds}")
    @RequiresPermissions("store:delete")
    public void deleteStores(@NotBlank(message = "{required}") @ApiParam(value = "商户id", required = true) @PathVariable String storeIds) throws FebsException {
        try {
            String[] ids = storeIds.split(StringPool.COMMA);
            this.storeService.deleteStores(ids);
        } catch (Exception e) {
            message = "删除商户失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改商户")
    @PutMapping
    @RequiresPermissions("store:update")
    public void updateStore(@Valid @ApiParam("需更新的商户对象") @RequestBody Store store) throws FebsException {
        try {
            this.storeService.updateStore(store);
        } catch (Exception e) {
            message = "修改商户失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @PostMapping("excel")
    @RequiresPermissions("store:export")
    public void export(QueryRequest queryRequest, Store store, HttpServletResponse response) throws FebsException {
        try {
            List<Store> stores = this.storeService.findStores(store, queryRequest).getRecords();
            ExcelKit.$Export(Store.class, response).downXlsx(stores, false);
        } catch (Exception e) {
            message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
